<?php
namespace app\client\controller;
class ChargeOrder extends Base {
    public function __construct() {
        parent::__construct();
    }
    
    public function charging() {
        if($this->noLogin()) {
            return $this->noLoginReturn();
        }
        
        $orderList = model('ChargeOrder')->findAll(['user_id' => $this->userid, 'status' => 1], ['order_no', 'order_id', 'device_no', 'port_no', 'start_time', 'charge_quantity', 'charge_type']);
        $time = time();
        
        $orderIds = [];
        
        foreach ($orderList as $order) {
            if(!in_array($order['order_id'], $orderIds)) {
                array_push($orderIds, $order['order_id']);
            }
        }
        
        $ports = model('DevicePort')->findAll(['current_order_id' => $orderIds], ['surplus_time', 'surplus_energy', 'power', 'current_order_id']);
        
        foreach ($orderList as &$order) {
            foreach ($ports as $k => $port) {
                if($order['order_id'] == $port['current_order_id']) {
                    unset($port['current_order_id']);
                    $order['data'] = $port;
                    unset($ports[$k]);
                    break;
                }
            }
        }
        
        return successResult(['list' => $orderList, 'current_time' => time()]);
    }
    
    public function orderList() {
        if($this->noLogin()) {
            return $this->noLoginReturn();
        }
        
        $params = request()->get();
        $pk = isset($params['lastPk']) ? intval($params['lastPk']) : 0;
        $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : 20;
        
        $where = [['user_id', '=', $this->userid], ['status', '=', 2]];
        
        if($pk > 0) {
            $where[] = ['order_id', '<', $pk];
        }
        
        $orderList = model('ChargeOrder')->where($where)->order('order_id desc')->field(['order_no', 'order_id', 'device_no', 'port_no', 'consume_amount', 'create_time'])->limit($pageSize)->select()->toArray();
        return successResult(['list' => $orderList]);
    }
    
    public function getOrderStatus() {
        if($this->noLogin()) {
            return $this->noLoginReturn();
        }
        $orderId = request()->get('id', 0, 'intval');
        if($orderId <= 0) {
            return errorResult('cc');
        }
        
        $status = model('ChargeOrder')->where(['order_id' => $orderId])->value('status');
        return successResult(['status' => $status]);
    }
    
    public function detail() {
        if($this->noLogin()) {
            return $this->noLoginReturn();
        }
        //0：充满自停，1：时间/度数-跑满，2：插头断开，3：超功率，-1：手动停止
        $reason = [-1 => '手动停止', 0 => '充满自停', 1 => '时间/度数-跑满', 2 => '插头断开', 3 => '超功率'];
        
        $orderNo = request()->get('no', '', 'trim');
        if($orderNo === '') {
            return errorResult('cc');
        }
        $field = ['user_id', 'device_no', 'port_no', 'charge_type', 'charge_quantity', 'amount', 'consume_amount', 'consume_time', 'consume_energy', 'avg_power', 'max_power', 'stop_reason', 'refund_amount', 'start_time', 'end_time'];
        $order = model('ChargeOrder')->findOne(['order_no' => $orderNo], $field);
        if(empty($order) || $order['user_id'] != $this->userid) {
            return errorResult('ddd');
        }
        $order['stop_reason_text'] = $reason[$order['stop_reason']];
        unset($order['user_id']);
        
        return successResult($order);
        
    }
}









